<template>
  <div id="newProject">
    <div class="titleTop">
      <div class="titleLeft">
        <i class="iconfont icontongyong_dangqianweizhi"></i>
        <span>项目</span>
        <i class="el-icon-arrow-right"></i>
        <span class="active">新增项目</span>
      </div>
    </div>
    <div class="message">基本信息</div>
    <div class="messageCon">
      <div class="box">
        <el-row :gutter="20" class="swDetail">
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">发行方式：</span>
            </span>
            <el-select v-model="data.pubMethod" placeholder="请选择" size="small">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">项目编号：</span>
            </span>
            <el-input v-model="data.sNo" placeholder="请输入项目编号" size="small" class="xmbh"></el-input>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="swDetail">
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">项目名称：</span>
            </span>
            <el-input v-model="data.projectName" placeholder="请输入项目名称" size="small" class="xmbh"></el-input>
          </el-col>
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">发行场所：</span>
            </span>
            <!-- <el-input v-model="data.pubLocation" placeholder="请输入发行场所" size="small" class="xmbh"></el-input> -->
            <el-select v-model="data.pubLocation" placeholder="请选择" size="small">
              <el-option
                v-for="item in fxcs"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="swDetailLast">
          <el-col :span="24">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">项目概述：</span>
            </span>
            <el-input
              class="xmbh"
              type="textarea"
              placeholder="请输入项目概述"
              :autosize="{ minRows: 4}"
              v-model="data.proDesc"
            ></el-input>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="swDetail">
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">&emsp;负责人：</span>
            </span>
            <el-input v-model="data.principal" placeholder="请输入负责人" size="small" class="xmbh"></el-input>
          </el-col>
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">项目成员：</span>
            </span>
            <el-input v-model="data.teamMember" placeholder="请输入项目成员" size="small" class="xmbh"></el-input>
          </el-col>
        </el-row>
        <el-row :gutter="20" class="swDetail">
          <!-- <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">&emsp;创建者：</span>
            </span>
            <el-input v-model="data.creator" :disabled="true" placeholder="请输入创建者" size="small" class="xmbh"></el-input>
          </el-col>-->
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">项目邮箱：</span>
            </span>
            <el-input v-model="data.mailBox" placeholder="请输入项目邮箱" size="small" class="xmbh"></el-input>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="20" class="swDetail">
          <el-col :span="12">
            <span class="spanBox">
              <span class="red">*</span>
              <span class="text">设立日期：</span>
            </span>
            <el-date-picker v-model="data.setupTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择项目设立日期" class="xmbh" size="small"></el-date-picker>
          </el-col>
        </el-row>-->
      </div>
    </div>
    <div class="message two">交易模式</div>
    <div class="moshi">
      <div class="moshiCon">
        <div class="left">
          <div class="first">
            <span
              v-for="(item, index) in moshiArr"
              :key="index"
              @click="currentIndex(index)"
              :class="[activeIndex == index? 'active' : '' ]"
            >{{item}}</span>
          </div>
        </div>
        <div class="right">
          <div class="imgbox">
            <img src="../../assets/images/danbao.png" />
          </div>
          <!-- <el-tooltip class="item" effect="dark" :content="arr1.join(',')? arr1.join(','): '未选择公司'" placement="top">
            <div class="danbao" @click="chooseCom('担保方')"></div>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" :content="arr2.join(',')? arr2.join(','): '未选择公司'" placement="top">
            <div class="xiangmu" @click="chooseCom('项目公司')"></div>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" :content="arr3.join(',')? arr3.join(','): '未选择公司'" placement="top">
            <div class="gongying" @click="chooseCom('供应商')"></div>
          </el-tooltip>-->
          <div class="Rbtn">
            <el-tooltip
              class="item"
              effect="dark"
              :content="arr1.join(',')? arr1.join(','): '未选择公司'"
              placement="top"
            >
              <el-button type="primary" size="medium" @click="chooseCom('担保方')">选择担保方</el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              :content="arr2.join(',')? arr2.join(','): '未选择公司'"
              placement="top"
            >
              <el-button type="primary" size="medium" @click="chooseCom('项目公司')">选择项目公司</el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              :content="arr3.join(',')? arr3.join(','): '未选择公司'"
              placement="top"
            >
              <el-button type="primary" size="medium" @click="chooseCom('供应商')">选择供应商</el-button>
            </el-tooltip>
          </div>
        </div>
      </div>
    </div>
    <div class="btnBottom">
      <button @click="submit">提交</button>
    </div>
    <!-- ---------------弹窗------------------------------ -->
    <el-dialog :visible.sync="dialogTable" width="900px" :title="type" center>
      <div class="swdyTops">
        <span class="search">搜索公司：</span>
        <el-input class="elinput" size="small" placeholder="请输入公司关键字" v-model="search"></el-input>
        <span class="sousuo">搜索不到？</span>
        <el-button class="gocreat" size="small" type="primary" @click="goChuangjian">去创建</el-button>
        <el-button
          class="gocreat"
          style="margin-left: 40px;"
          size="small"
          type="success"
          @click="dialogTable = false"
        >完成选择</el-button>
      </div>
      <div class="diatable">
        <el-table
          :data="diatableData"
          border
          style="width: 100%"
          :row-class-name="tableRowClassName"
        >
          <el-table-column prop="clientId" label="序号" align="center"></el-table-column>
          <el-table-column prop="companyName" label="客户名称" align="center"></el-table-column>
          <!-- <el-table-column prop="lsCheck" label="中登查询" align="center"></el-table-column>
          <el-table-column prop="regDate" label="中登更新时间" align="center"></el-table-column>-->
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                v-if="!scope.row.companyRole"
                @click="choose(scope.row)"
              >选择</el-button>
              <el-button
                type="danger"
                size="mini"
                v-else
                @click="delCom(scope.row)"
              >删除({{scope.row.companyRole}})</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="10"
            layout="total, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="already" width="900px"></el-dialog>
  </div>
</template>

<script>
export default {
  name: "newProject",
  data() {
    return {
      moshiArr: [
        "交易模式一",
        "交易模式二",
        "交易模式三",
        "交易模式四",
        "交易模式五",
        "交易模式六"
      ],
      activeIndex: null,
      options: [
        {
          value: "单笔项目",
          label: "单笔项目"
        },
        {
          value: "储架式",
          label: "储架式"
        }
      ],
      fxcs: [
        {
          value: "银行",
          label: "银行"
        },
        {
          value: "交易所",
          label: "交易所"
        }
      ],
      data: {
        pubMethod: "",
        sNo: "",
        projectName: "",
        pubLocation: "",
        proDesc: "",
        principal: "",
        teamMember: "",
        mailBox: ""
        // tradePattern: "",
        // setupTime: ""
      },
      value: "",
      textarea: "",
      dialogTable: false,
      diatableData: [],
      currentPage: "",
      currentPage: "",
      total: 0,
      search: "",
      type: "",
      projectClients: [],
      arr1: [],
      data1: [],

      arr2: [],
      // tip2: '',
      data2: [],
      arr3: [],
      // tip3: '',
      data3: [],
      // ------------------------
      already: false
    };
  },
  methods: {
    delCom(row) {
      // companyName
      if (row.companyRole == "担保方") {
        let index = this.fdIndex(row, this.arr1);
        this.arr1.splice(index, 1);
        this.data1.splice(index, 1);
      } else if (row.companyRole == "项目公司") {
        let index = this.fdIndex(row, this.arr2);
        this.arr2.splice(index, 1);
        this.data2.splice(index, 1);
      } else if (row.companyRole == "供应商") {
        let index = this.fdIndex(row, this.arr3);
        this.arr3.splice(index, 1);
        this.data3.splice(index, 1);
      }
      let index = this.projectClients.findIndex((item, index) => {
        return item.clientId == row.clientId;
      });
      this.projectClients.splice(index, 1);
      row.companyRole = "";
      this.$message({
        message: "已移除" + row.companyName,
        type: "success"
      });
    },
    fdIndex(row, arr) {
      console.log(row, arr);
      let index = arr.findIndex((item, index) => {
        return item == row.companyName;
      });
      console.log(index);
      return index;
    },
    tableRowClassName({ row, rowIndex }) {
      if (row.companyRole == "担保方") {
        return "dbf";
      } else if (row.companyRole == "项目公司") {
        return "xmgs";
      } else if (row.companyRole == "供应商") {
        return "gys";
      }
      return "";
    },
    chooseCom(val) {
      this.type = val;
      this.dialogTable = true;
      this.getList(1);
    },
    choose(data) {
      let bool = this.projectClients.some((item, index) => {
        return data.clientId == item.clientId;
      });
      if (bool) {
        this.$message({
          message: "您已经选过此客户请勿重复选择",
          type: "warning"
        });
        return false;
      }
      if (this.type == "担保方") {
        if (this.arr1.length >= 1) {
          this.$message({
            message: "担保方只能选择一个",
            type: "warning"
          });
          return false;
        }
        this.arr1.push(data.companyName);
        this.$message({
          message: "你已选择" + data.companyName,
          type: "success"
        });
        data.companyRole = "担保方";
        this.data1.push({
          clientId: data.clientId,
          companyName: data.companyName,
          companyRole: this.type
        });
      }
      if (this.type == "项目公司") {
        this.arr2.push(data.companyName);
        this.$message({
          message: "你已选择" + data.companyName,
          type: "success"
        });
        data.companyRole = "项目公司";
        this.data2.push({
          clientId: data.clientId,
          companyName: data.companyName,
          companyRole: this.type
        });
      }
      if (this.type == "供应商") {
        this.arr3.push(data.companyName);
        this.$message({
          message: "你已选择" + data.companyName,
          type: "success"
        });
        data.companyRole = "供应商";
        this.data3.push({
          clientId: data.clientId,
          companyName: data.companyName,
          companyRole: this.type
        });
      }
      // 后端需要的数据格式
      this.projectClients = this.data1.concat(this.data2).concat(this.data3);
    },
    getList(val) {
      this.currentPage = val;
      let obj = {
        companyName: this.search,
        page: this.currentPage
      };
      this.axios
        .get("AdminAPI/project/showClientListForProject", {
          params: this.filterNoneObj(obj)
        })
        .then(({ data }) => {
          console.log(data);
          this.diatableData = data.data;
          this.total = data.pageInfo.total;
        });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList(val);
    },
    goChuangjian() {
      this.$router.push("home/enterInfo");
    },
    currentIndex(index) {
      this.activeIndex = index;
    },
    submit() {
      let obj = this.data;
      for (let k in obj) {
        if (!obj[k]) {
          this.$message({ message: "请您填写完整", type: "warning" });
          return false;
        }
      }
      if (this.activeIndex !== null) {
        obj.tradePattern = this.moshiArr[this.activeIndex];
      }
      obj.projectClients = this.data1.concat(this.data2).concat(this.data3);
      this.axios.post("AdminAPI/project/addProject", obj).then(({ data }) => {
        this.$message({ message: "提交成功", type: "success" });
        this.$router.push("projectList");
      });
    }
  },
  watch: {
    search: function() {
      this.getList(1);
    },
    diatableData: function() {
      for (let value of this.projectClients) {
        this.diatableData.forEach(ele => {
          if (ele.clientId == value.clientId) {
            ele.companyRole = value.companyRole;
          }
        });
      }
    }
  }
};
</script>

<style lang="scss">
#newProject {
  .message {
    margin-top: 6px;
    height: 60px;
    background: rgba(226, 238, 253, 1);
    box-shadow: 0px 0px 2px 0px rgba(189, 189, 190, 0.5);
    border-radius: 6px;
    padding-left: 36px;
    font-size: 20px;
    color: #333333;
    line-height: 60px;
    font-weight: bold;
    &.two {
      margin-top: 40px;
    }
  }
  .messageCon {
    display: flex;
    justify-content: center;
    font-size: 18px;
    .box {
      width: 70%;
      .el-select {
        width: calc(100% - 126px);
      }
      .xmbh {
        width: calc(100% - 126px);
      }
      .red {
        color: red;
        font-size: 20px;
        font-weight: bold;
      }
      .swDetail {
        height: 70px;
        display: flex;
        align-items: center;
      }
      .swDetailLast {
        min-height: 70px;
        margin-top: 16px;
        .spanBox {
          display: inline-block;
          vertical-align: top;
        }
        textarea {
          resize: none;
        }
      }
      .spanBox {
        min-width: 106px;
      }
    }
  }
  .moshi {
    padding: 20px 43px;
    .moshiCon {
      background: white;
      height: 400px;
      display: flex;
      .left {
        // width: 50%;
        flex: 1;
        // height: 364px;
        height: 400px;
        box-sizing: border-box;
        border-right: 1px solid #dfdcdf;
        display: flex;
        flex-direction: column;
        justify-content: center;
        .first {
          margin-bottom: 69px;
        }
        > div {
          display: flex;
          justify-content: space-around;
          flex-wrap: wrap;
          &:first-child {
            span {
              font-size: 18px;
              color: rgba(255, 255, 255, 1);
              margin-top: 60px;
              width: 26%;
              height: 39px;
              text-align: center;
              line-height: 39px;
              cursor: pointer;
              &.active {
                color: black;
                font-weight: 600;
              }
              &:nth-of-type(1) {
                background: rgba(240, 185, 145, 1);
                box-shadow: 0px 0px 4px 0px rgba(100, 100, 100, 0.5);
                border-radius: 10px;
              }
              &:nth-of-type(2) {
                background: rgba(255, 158, 154, 1);
                box-shadow: 0px 0px 4px 0px rgba(100, 100, 100, 0.5);
                border-radius: 10px;
              }
              &:nth-of-type(3) {
                background: rgba(249, 137, 173, 1);
                box-shadow: 0px 0px 4px 0px rgba(100, 100, 100, 0.5);
                border-radius: 10px;
              }
              &:nth-of-type(4) {
                background: rgba(121, 188, 238, 1);
                box-shadow: 0px 0px 4px 0px rgba(100, 100, 100, 0.5);
                border-radius: 10px;
              }
              &:nth-of-type(5) {
                background: rgba(97, 152, 211, 1);
                box-shadow: 0px 0px 4px 0px rgba(100, 100, 100, 0.5);
                border-radius: 10px;
              }
              &:nth-of-type(6) {
                background: rgba(56, 167, 243, 1);
                box-shadow: 0px 0px 4px 0px rgba(100, 100, 100, 0.5);
                border-radius: 10px;
              }
            }
          }
        }
      }
      .right {
        // width: 576px;
        // width: 100%;
        flex: 1;
        position: relative;
        text-align: center;
        overflow: hidden;
        .danbao {
          position: absolute;
          width: 150px;
          height: 56px;
          background: red;
          z-index: 3;
          left: 210px;
          top: 64px;
          opacity: 0;
          cursor: pointer;
        }
        .xiangmu,
        .gongying {
          position: absolute;
          height: 42px;
          width: 100%;
          background: red;
          z-index: 3;
          left: 0px;
          top: 154px;
          opacity: 0;
          cursor: pointer;
        }
        .gongying {
          top: 298px;
        }
        .imgbox {
          // background: pink;
          // width: 100%;
        }
      }
    }
  }
  .btnBottom {
    margin-top: 56px;
    text-align: center;
    padding-bottom: 20px;
    button {
      width: 632px;
      height: 50px;
      background: rgba(242, 146, 7, 1);
      box-shadow: 0px 0px 6px 0px rgba(122, 121, 121, 0.5);
      border-radius: 10px;
      font-size: 22px;
      color: white;
      cursor: pointer;
    }
  }
  .swdyTops {
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid #b9b9b9;
    padding-bottom: 26px;
    .el-input {
      width: 160px;
    }
    .sousuo {
      margin: 0 40px;
    }
  }
  .diatable {
    margin-top: 10px;
    background: white;
    padding-top: 16px;
    .has-gutter tr th {
      background: #fefaf1;
    }
    .block {
      text-align: right;
      margin-right: 55px;
      height: 50px;
      margin-top: 20px;
    }
  }
  .el-table .dbf {
    background: rgb(247, 194, 185);
    // background: red;
  }
  .el-table .xmgs {
    background: #f0f9eb;
    // background: green;
  }
  .el-table .gys {
    background: #d8e1fd;
    // background: pink;
  }
}
</style>
